package com.yyge.config;

import com.yyge.interceptor.GlobalInterceptor;
import com.yyge.interceptor.AuthInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class SpringMvcConfig implements WebMvcConfigurer {

    @Autowired
    @Qualifier("globalInterceptor")
    private GlobalInterceptor globalInterceptor;

    @Autowired
    private AuthInterceptor authInterceptor;

    /**
     * 跨域
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
           .allowedOrigins("*")
           .allowedHeaders("*")
           .allowedMethods("*")
           .allowCredentials(false)
           .maxAge(1800);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 多个拦截器的执行顺序addInterceptor顺序执行
        //使用拦截器
        //registry.addInterceptor(globalInterceptor) 意思是添加拦截器globalInterceptor
//        registry.addInterceptor(globalInterceptor)
//                .addPathPatterns("/**");
        registry.addInterceptor(authInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/login","/captcha", "/swagger-resources/**","/swagger-ui/**", "/v3/**","/front/**");
        /**
         *
         * Swageer放行通道
         * /swagger-resources/**
         * /swagger-ui/**
         * /v3/**
         */

        //http://localhost:8080/api/swagger-ui/index.html
    }

//    /**
//        静态资源放行
//        @param registry
//    */
//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry){
//          //放行Swagger3
//         registry.addResourceHandler("swagger-ui.html")
//         .addResourceLocations("classpath:/META-INF/resources/");
//    }


    /**
        静态资源放行
        @paramregistry
     */
//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry){
//      registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
//      registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
//    }
}